/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#ifndef ___IMAGEDISPLAY_HPP
#define ___IMAGEDISPLAY_HPP

#define GIMI_CLIENT_API (20000)
#include "gimi.h"
#include "ImageClient.hpp"

#include <GL/gl.h>

class CImageDisplay : public MaCI::Image::CImageClientCallback
{
public:
    
  static const int infoAreaHeight;
    
  CImageDisplay();
    
  ~CImageDisplay();
    
  /** DecreaseImageContainerIndex
   *
   */
  void DecreaseImageContainerIndex();

  /** IncreaseImageContainerIndex
   *
   */  
  void IncreaseImageContainerIndex();


  /** Toggles flipping of the image (up/down)
   *
   */
  void ToggleFlip();

  /** GetImageContainerIndex
   *
   * Returns the index of the current image container
   */
  const int GetImageContainerIndex()
  {
    return imageContainerIndex;
  };
  
  /** GetStreamCount
   *
   * Returns the total number of image containers available
   */
  const int GetStreamCount()
  {
    return iStreamCount;
  };

  /** Draw
   * 
   * Draws the image using a textured quad. Quad aspect ratio is same as image's.
   * The longer side of image will be set to 1 in gl coordinates. Quad lower 
   * left corner is placed at (0,0)
   */
  void Draw(int imageIndex=0);
    
  /** DisplayInfo
   * 
   * Define if a info field should be displayed in the picture
   */
  void DisplayInfo(bool info);
    
  /** DisplayWarnings
   * 
   * Define if warnings about slow frame rate or disconnect should be shown
   */
  void DisplayWarnings(bool warnings);
    
  /** GetWidth
   * 
   * Returns image width in pixels
   */
  int GetWidth();
    
  /** GetHeight
   * 
   * Returns image height in pixels
   */
  int GetHeight();
    
  /** Connect
   * 
   */
  bool Connect(gimi::GIMI *gimi, int gimnetID, int minor);
    
  void Disconnect();
    
  //callback for image client
  void OnImageDataEvent(const MaCI::Image::CImageData &aData, 
                        const unsigned int aDataSequence, 
                        MaCI::Image::CImageClient &aImageClient);
    
  bool IsConnected() {return connected;}
    
protected:
    
private:
  GLuint texture;
  double imageRatio;
  int imageWidth, imageHeight, texWidth, texHeight;
  int imageContainerIndex;
  bool iFlipImage;
  int iStreamCount;
  //image cords on texture (whole texture is bigger than received image)
  double texCoordX,texCoordY;
  //quad dimensions
  double vertexX, vertexY;
  //if image size has changed
  bool sizeChanged;
    
  unsigned char *RGBData;
  ownMutexHandle imageMutex;
    
  bool connected;  //if connect has been tried
  bool opened;  //if connect was succesful
    
  bool displayInfo, displayWarnings;
  gim::time lastImageTime;
    
  MaCI::Common::TTimestamp lastTimeStamp;
    
  MaCI::Image::CImageClient *imageClient;
    
  void drawInfo(std::string message);
    
};

#endif
